package cn.bxshey.modules_commodity.controller;

import cn.bxshey.general.Result.Result;
import cn.bxshey.general.aop.note.SystemLog;
import cn.bxshey.general.base.BaseController;
import cn.bxshey.modules_commodity.dto.commodity.*;
import cn.bxshey.modules_commodity.service.CommodityService;
import cn.bxshey.modules_commodity.vo.commodity.CartDetailVO;
import cn.bxshey.modules_commodity.vo.commodity.DetailVO;
import cn.bxshey.modules_commodity.vo.commodity.PaginatedQueriesVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @作者：不想说话而已
 * @Date：2024/2/1 10:42
 * 商品服务
 */

@RestController
@RequestMapping("/commodity")
@RefreshScope
public class CommodityController extends BaseController {

    @Resource
    CommodityService commodityService;

    /**
     * 商品服务测试
     *
     * @return String
     */
    @GetMapping("/test")
    @SystemLog(value = "商品服务测试", serve = "1003")
    public Result<String> testDemo(String s) {

        getUserLoginInfo();

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = requestAttributes.getRequest();
            System.out.println(request.getHeader("feign-token"));
            System.out.println(request.getHeader("Authorization"));
        }
        return Result.success("商品服务 测试成功！---->" + s);
    }

    /**
     * 分页查询商品列表
     *
     * @param dto 分页查询参数
     * @return 商品列表
     */
    @GetMapping("/queryPage")
    @SystemLog(value = "分页查询商品列表", serve = "1003")
    public Result<List<PaginatedQueriesVO>> queryPageCommodity(PaginatedQueriesDTO dto) {
        return Result.success(commodityService.queryPage(dto));
    }

    /**
     * 新增商品
     *
     * @param dto 新增商品参数
     * @return 成功个数
     */
    @PostMapping("/add")
    @SystemLog(value = "新增商品", serve = "1003")
    public Result<Integer> addCommodity(@RequestBody AddCommodityDTO dto) {
        return Result.success(commodityService.addCommodity(dto));
    }

    /**
     * 新增商品规格
     *
     * @param dto 新增商品规格参数
     * @return 成功个数
     */
    @PostMapping("/addSpecification")
    @SystemLog(value = "新增商品规格", serve = "1003")
    public Result<Integer> addCommoditySpecification(@RequestBody AddCommoditySpecificationDTO dto) {
        return Result.success(commodityService.addCommoditySpecification(dto));
    }


    /**
     * 更新商品(不更新规格)
     *
     * @param dto 更新商品参数
     * @return 成功个数
     */
    @PutMapping("/update")
    @SystemLog(value = "更新商品(不更新规格)", serve = "1003")
    public Result<Integer> updateCommodity(@RequestBody UpdateCommodityDTO dto) {
        return Result.success(commodityService.updateCommodity(dto));
    }

    /**
     * 更新商品规格
     *
     * @param dto 更新商品规格参数
     * @return 成功个数
     */
    @PutMapping("/updateSpecification")
    @SystemLog(value = "更新商品规格", serve = "1003")
    public Result<Integer> updateCommoditySpecification(@RequestBody UpdateCommoditySpecificationDto dto) {
        return Result.success(commodityService.updateCommoditySpecification(dto));
    }

    /**
     * 根据id查询商品详情
     *
     * @param id 商品id
     * @return 商品详情内容
     */
    @GetMapping("/{id}")
    @SystemLog(value = "根据id查询商品详情", serve = "1003")
    public Result<DetailVO> commodityDetail(@PathVariable("id") String id) {
        return Result.success(commodityService.commodityById(id));
    }

    /**
     * 加入购物车
     * @param: 商品id,规格id,数量
     * @return: turn
     */
    @PostMapping("/addCart")
    @SystemLog(value = "加入购物车", serve = "1003")
    public Result<Long> addCart(@RequestBody AddCartDTO dto) throws JsonProcessingException {
        Object uid = getUserLoginInfo().get("uid");
        return Result.success(commodityService.addCart(String.valueOf(uid),dto));
    }

    /**
     * 获取购物车信息
     * @return: 购物车信息
     */
    @GetMapping("/cartDetail")
    @SystemLog(value = "获取购物车信息", serve = "1003")
    public Result<List<CartDetailVO>> cartDetail() throws IOException {
        Long uid = Long.parseLong(String.valueOf(getUserLoginInfo().get("uid")));
        return Result.success(commodityService.cartDetail(uid));
    }

    /**
     * 购物车结算
     * @param: 商品id、商品规格id、商品数量
     * @return: 购物车信息
     */
    @PostMapping("/settlement")
    @SystemLog(value = "购物车结算", serve = "1003")
    public Result<?> settlement(@RequestBody List<SettlementDTO> settlementDTOS){
        Long uid = Long.parseLong(String.valueOf(getUserLoginInfo().get("uid")));

        return Result.success(commodityService.settlement(uid,settlementDTOS));
    }


    @GetMapping("/addCoupons")
    public Result<?> addCoupons(@RequestBody CouponsDTO couponsDTO){
        Long uid = Long.parseLong(String.valueOf(getUserLoginInfo().get("uid")));

        return Result.success();
    }
 
}
